package com.gitee.wsl.ext.string

/**
 * Returns a copy of this string having its first letter title-cased using the rules of the default
 * locale, or the original string if it's empty or already starts with a title case letter.
 *
 * The title case of a character is usually the same as its upper case with several exceptions. The
 * particular list of characters with the special title case form depends on the underlying
 * platform.
 */
fun String.capitalize(): String = replaceFirstChar {
    if (it.isLowerCase()) it.titlecase() else it.toString()
}

/**
 * Returns a copy of this string having its first letter lowercased using the rules of the default
 * locale, or the original string if it's empty or already starts with a lower case letter.
 */
fun String.decapitalize(): String = replaceFirstChar { it.lowercase() }

fun String.decapitalizeAsciiOnly(): String {
    if (isEmpty()) return this
    val c = this[0]
    return if (c in 'A'..'Z') {
        c.lowercaseChar() + substring(1)
    } else {
        this
    }
}

fun String?.firstCharToUpperCase(): String {
  return when {
    this?.isEmpty() != false -> ""
    length == 1 -> uppercase()
    else -> substring(0, 1).uppercase() + substring(1)
  }
}




